package rabid;

import java.util.HashMap;

public class Browse {

	public String[] input;
	public HashMap hashMap;
	public String[] fileNames;
	public int numFiles;
	int[][] files;
	public String[] results;
	public Cluster cluster;
	
	public Browse(String[] toBrowse, HashMap hash, String[] corpusFiles, int corpusSize){
		input = toBrowse;		
		hashMap = hash;
		fileNames = corpusFiles;
		numFiles = corpusSize;
		browseInput();
	}
	
	public void browseInput() {
		
		if (input.length > 0) {
			Query query = new Query(input, hashMap, fileNames, numFiles);
			String[] files = query.getFiles();
			boolean[] filesIncluded = getUsedFiles(files);
			cluster = new Cluster(hashMap, fileNames, numFiles, filesIncluded);
		} else {
			boolean[] files = new boolean[numFiles];
			files = fill(files, true);
			cluster = new Cluster(hashMap, fileNames, numFiles, files);
		}
	}

	/**
	 * @param files2
	 */
	private boolean[] getUsedFiles(String[] filesUsed) {
		boolean[] toReturn = new boolean[numFiles];
		int i=0;
		while(i<fileNames.length){
			toReturn[i] = includes(filesUsed, fileNames[i]);
			i++;
		}
		return toReturn;
		
	}

	/**
	 * @param filesUsed
	 * @param fileNames2
	 * @return
	 */
	private boolean includes(String[] filesUsed, String fileName) {
		for(int i=0; i<filesUsed.length; i++){
			if(filesUsed[i].compareTo(fileName) == 0){
				return true;
			}
		}
		return false;
	}

	/**
	 * @param files
	 * @param b
	 */
	private boolean[] fill(boolean[] files, boolean b) {
		for(int i=0; i<files.length; i++){
			files[i] = b;
		}
		return files;
	}
	
	public String[] getResults(){
		return cluster.results;
	}
	
}
